package com.ceres.crops.controller;


import com.ceres.crops.pojo.Pest;
import com.ceres.crops.service.CropsService;
import com.ceres.crops.service.PestService;
import com.ceres.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pest")
@SuppressWarnings("all")
public class PestController {

    @Autowired
    private PestService pestService;

    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<Pest> pestList = pestService.findAll();
            return new Result(true,2000,pestList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,2001);
        }
    }

    @PutMapping("/update")
    public Result update(@RequestBody Pest pest,Integer id){
        try {
            pest.setId(id);
            pestService.update(pest);
            return new Result(true,2000);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,2001);
        }
    }

    /*@DeleteMapping("/del")
    public Result del(Integer id){
        try {
            pestService.del(id);
            return new Result(true,2000);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,2001);
        }
    }*/

    @PostMapping("/add")
    public Result add(@RequestBody Pest pest){
        try {
            pestService.add(pest);
            return new Result(true,2000);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,2001);
        }
    }
}
